package com.example.myanimator.view;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.myanimator.R;

/**
 * @author tgw
 * @date 2021/3/16
 * @describe
 */
public class CustomCircleViewCanvas extends View {

    private Paint mPaint;
    private Path mPath;
    private Canvas mCanvas;
    private Bitmap mBitmap;
    private int width;
    private int height;

    private int CLIP_HEIGHT = 30;
    private int clipWidth = 0;
    private Region mRegion;


    public CustomCircleViewCanvas(Context context) {
        super(context);
    }

    /**通过xml使用该控件 ，必须实现该构造*/
    public CustomCircleViewCanvas(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        //使用裁剪要禁用软件加速
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPath = new Path();
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.aa);
        //这个缩小bitmap大小
        mBitmap = thumbImageWithInSampleSize(100,100);
        width = mBitmap.getWidth();
        height = mBitmap.getHeight();

        mRegion = new Region();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initCircle(canvas);

        //有问题
//        initCircleBook(canvas);
    }

    private void initCircle(Canvas canvas) {
        mPath.addCircle(100, 100, 50, Path.Direction.CCW);
        int c1 = canvas.save();//保存当前画布大小，即整屏
        canvas.translate(50,200);

        canvas.clipPath(mPath);
        canvas.drawBitmap(mBitmap, -50, 0, mPaint);
//        canvas.translate(0,200);
//        canvas.drawBitmap(mBitmap, 0, 0, mPaint);

        canvas.restore();
    }

    public Bitmap thumbImageWithInSampleSize(float destWidth, float destHeight) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;

        BitmapFactory.decodeResource(getResources(),  R.mipmap.aa, opt);
        double scaleW = Math.max(destWidth, opt.outWidth) / (Math.min(destWidth, opt.outWidth) * 1.0);
        double scaleH = Math.max(destHeight, opt.outHeight) / (Math.min(destHeight, opt.outHeight) * 1.0);
        opt.inSampleSize = (int) Math.max(scaleW, scaleH);

        opt.inJustDecodeBounds = false;

        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.aa, opt);
        return mBitmap;
    }
    private void initCircleBook(Canvas canvas) {
//        mRegion.setEmpty();
//        int i =0;
//        while (i*CLIP_HEIGHT<=height){//计算clip区域
//            if (i*2 ==0){
//                mRegion.union(new Rect(0,i*CLIP_HEIGHT,clipWidth,(i+1)*CLIP_HEIGHT));
//            }else {
//                mRegion.union(new Rect(width-clipWidth,i*CLIP_HEIGHT,width,(i+1)*CLIP_HEIGHT));
//
//            }
//            i++;
//        }
//        canvas.clipreg
    }

}
